//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Policy Injection Application Block
//===============================================================================

using System.Collections;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace EntLibContrib.PolicyInjection.CallHandlers
{
	/// <summary>
	/// Extends IMethodInvocation
	/// </summary>
	public static class MethodInvocationExtensions
	{
		#region Fields
		private static readonly object instanceInvocationContextKey = new object();
		#endregion

		#region Public Methods
		/// <summary>
		/// Sets the Invocation Context that will be shared by all method invocations on a given instance.
		/// </summary>
		/// <param name="invocation">Current Invocation Context</param>
		/// <param name="instanceContext">Instance Invocation Context</param>
		public static void SetInstanceContext(IMethodInvocation invocation, IDictionary instanceContext)
		{
			invocation.InvocationContext[instanceInvocationContextKey] = instanceContext;
		}

		/// <summary>
		/// Gets the current Instance Invocation Context.
		/// </summary>
		/// <param name="invocation">Invocation method</param>
		/// <returns>
		/// Instance Invocation context is present. null otherwise.
		/// </returns>
		public static IDictionary GetInstanceContext(IMethodInvocation invocation)
		{
			return (IDictionary)invocation.InvocationContext[instanceInvocationContextKey];
		}
		#endregion
	}
}
